import requests
from pandas import DataFrame
from PySide6.QtCore import QThread, Signal


class ExportExcelThread(QThread):
    success_signal = Signal(str)
    error_signal = Signal(str)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.data = []
        self.filename = None

    def set_data(self, data):
        self.data = data

    def set_filename(self, filename):
        self.filename = filename

    def run(self):
        if not self.filename:
            return
        try:
            df = DataFrame(self.data)
            df.to_excel(self.filename, header=True, index=False)
        except Exception as e:
            self.error_signal.emit(str(e))
        else:
            self.success_signal.emit('文件保存至:{}'.format(str(self.filename)))


